#!/usr/bin/python 
# -*- coding: UTF-8 -*-
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
import datetime
from wikicamp.smart_selects.db_fields import ChainedForeignKey

# Create your models here.
# Create your models here
class MyUser(User):
    class Meta:
        proxy = True

    def __unicode__(self):
        try:
            if len(self.get_full_name()) < 3:
                return self.username
            else:
                return self.get_full_name()
        except:
            return "-hata-"
        
class gecerlilik_durumlar(models.Model):
    sno= models.IntegerField()
    aciklama = models.CharField(max_length=100)
        
    def __unicode__(self):
        return self.aciklama
    
class kullanicigrup(models.Model):
    sno = models.IntegerField()
    aciklama = models.CharField(max_length=150)
    durum = models.ForeignKey(gecerlilik_durumlar)
    
    def __unicode__(self):
        return self.aciklama
    
class atolyeler(models.Model):
    sno= models.IntegerField()
    aciklama= models.CharField(max_length=150)
    durum = models.ForeignKey(gecerlilik_durumlar)
    
    def __unicode__(self):
        return self.aciklama
    
class yetki(models.Model):
    sno = models.IntegerField()
    aciklama = models.CharField(max_length=100)
    goruntule = models.BooleanField()
    duzelt = models.BooleanField()
    olustur = models.BooleanField()
    sil = models.BooleanField()
    def __unicode__(self):
        return self.aciklama



class isturu(models.Model):
    sno = models.IntegerField()
    aciklama = models.CharField(max_length=80)
    
    def __unicode__(self):
        return self.aciklama

class lokasyonlar(models.Model):
    sno = models.IntegerField()
    aciklama = models.CharField(max_length=100)
    durum = models.ForeignKey(gecerlilik_durumlar)
    
    def __unicode__(self):
        return self.aciklama

class caritip(models.Model):
    sno = models.IntegerField()
    aciklama = models.CharField(max_length=50)
    
    def __unicode__(self):
        return self.aciklama
    
class aidiyet(models.Model):
    sno = models.IntegerField()
    aciklama = models.CharField(max_length=50)
    
    def __unicode__(self):
        return self.aciklama

    
class carikart(models.Model):       
    sno = models.IntegerField()
    aciklama = models.CharField(max_length=250,blank=True,null=True)
    adres = models.CharField(max_length=450,blank=True,null=True)
    tel1 = models.CharField(max_length=20,blank=True,null=True)
    tel2 = models.CharField(max_length=20,blank=True,null=True)
    iletisim = models.CharField(max_length=150,blank=True,null=True)
    email = models.EmailField(max_length=200,blank=True,null=True)
    tip = models.ForeignKey(caritip)
    
    def __unicode__(self):
        return unicode(self.aciklama)


class cihazlar(models.Model):
    sno = models.IntegerField()
    aciklama = models.CharField(max_length=100)
    serino = models.CharField(max_length=100)
    lokasyon = models.ForeignKey(lokasyonlar)
    modeli = models.CharField(max_length=150,blank=True,null=True)
    demirbasno = models.CharField(max_length=100,blank=True,null=True)
    durum = models.ForeignKey(gecerlilik_durumlar)
    markasi = models.ForeignKey(carikart,related_name="markasi",blank=True,null=True)
    eknot = models.CharField(max_length=200,blank=True,null=True)
    parent_id = models.IntegerField(blank=True,null=True)
    aidiyet = models.ForeignKey(aidiyet)
    barkod = models.CharField(blank=True,null=True,max_length=150)    
    garanti_bitis_tarihi = models.DateTimeField('warranty_toend',blank=True,null=True)
    yetkili_servisi=models.ForeignKey(carikart,related_name="yetkili_servisi",blank=True,null=True)
    cihaz_alinis_tarihi = models.DateField('purchase_date',blank=True,null=True)
    
    def __unicode__(self):
        try:
            return str(self.sno) + "-" + self.aciklama + " [m: "+ unicode(carikart.objects.get(id=self.markasi_id).aciklama).encode('ascii')+"][s: " + self.serino[0:20] +"..]"
        except:
            return str(self.sno) + "-" + self.aciklama + " [s: " + self.serino[0:20] +"..]"


class kullanicilar(models.Model):
    sno = models.IntegerField()
    user = models.ForeignKey(User,unique=True)
    ack = models.TextField(max_length=250,blank=True,null=True)
    unvan = models.CharField(max_length=20,blank=True,null=True)
    atolye = models.ManyToManyField(atolyeler,blank=True, null=True)
    timestamp = models.DateTimeField('date_created',blank=True,null=True)
    
    class Meta:
        permissions = (
            ("kendi_kayitlarini_silme", "Sadece Kendi Kayitlarini Silebilir"),
            ("is_kisi_edit","Is atanan kisiyi degistirebilir"),
            ("is_durum_edit","Is durum degistirebilir"),
            ("is_tum_liste","Butun isleri listeyebilir"),
            ("sabit_tanimlama","Tanimlama bilgisi girebilir"),
            ("kullanici_acma","Kullanici acabilir"),
            ("is_kapatabilir","Is onaylama ve sonlandirma yetkisi"),
            ("coklu_onayla","Coklu is onaylayabilme ve sonlandirma yetkisi"),
            ("bakim_form_onay","Bakim formlarini onaylayabilme yetkisi"),
        )

    def __unicode__(self):
        return self.ack
    

class is_durumlar(models.Model):
    sno = models.IntegerField()
    aciklama = models.CharField(max_length=100)
    
    def __unicode__(self):
        return self.aciklama 


class islistesi(models.Model):
    sno = models.IntegerField()
    turu = models.ForeignKey(isturu)
    aciklama = models.CharField(max_length=250,blank=True,null=True)
    lokasyon = models.ForeignKey(lokasyonlar)
    #cihaz = models.ForeignKey(cihazlar,blank=True,null=True)
    cihaz= ChainedForeignKey(cihazlar,chained_field="lokasyon",chained_model_field="lokasyon",show_all=False,blank=True,null=True)
    atolye = models.ForeignKey(atolyeler)
    islem_durum = models.ForeignKey(is_durumlar)
    eknot = models.CharField(max_length=600,blank=True,null=True)
    planlanan_baslangic_tarihi = models.DateTimeField('date_planned_tostart')
    planlanan_bitis_tarihi = models.DateTimeField('date_planned_toend',blank=True,null=True)
    onaylayan_kullanici = models.ForeignKey(MyUser,related_name="onaylayan",blank=True,null=True)
    onaylandi = models.NullBooleanField(blank=True,null=True)
    emailbildirimleri = models.BooleanField() 
    kullanici = models.ForeignKey(MyUser,related_name="olusturan")
    timestamp = models.DateTimeField('date_created')
    tumgun =models.NullBooleanField(blank=True,null=True)
    url = models.URLField(blank=True,null=True)
    
    def __unicode__(self):
        return "id: #" + str(self.id) + "|"+ self.aciklama + " - " + str(self.timestamp) 

class bakimnoktalari(models.Model):
    sno= models.IntegerField()
    aciklama = models.CharField(max_length=350)
    
    def __unicode__(self):
        return self.aciklama

class bakimkaydi(models.Model):
    bakimnoktasi = models.ForeignKey(bakimnoktalari)
    durum = models.NullBooleanField(blank=True,null=True)
    eknot = models.CharField(blank=True,null=True,max_length=350)
    
    def __unicode__(self):
        try:
            return bakimnoktalari.objects.get(id=self.bakimnoktasi_id).aciklama
        except:
            return unicode(bakimnoktalari.objects.get(id=self.bakimnoktasi_id).aciklama).encode('ascii')
    
class bakimtaslagi(models.Model):
    aciklama = models.CharField(max_length=350)
    kayit = models.ManyToManyField(bakimkaydi)
    ontanimli = models.NullBooleanField(blank=True,null=True)
    
    def __unicode__(self):
        return self.aciklama

class bakimtablosu(models.Model):
    aciklama = models.CharField(max_length=350)
    taslak = models.ForeignKey(bakimtaslagi)
    kullanici = models.ForeignKey(MyUser)
    timestamp = models.DateTimeField('date_created') 
    cihaz = models.ForeignKey(cihazlar)
    islistesi = models.ForeignKey(islistesi,blank=True,null=True)
    onaylandi = models.NullBooleanField(blank=True,null=True)

class birim(models.Model):
    aciklama = models.CharField(max_length=50)
    def __unicode__(self):
        return self.aciklama
    
    
class fatura(models.Model):
    sno = models.IntegerField()
    aciklama = models.CharField(max_length=150,blank=True,null=True)
    fatura_no = models.CharField(max_length=8)
    fatura_tarih = models.DateField()
    cihaz = models.ForeignKey(cihazlar,blank=True,null=True)
    isistek = models.ForeignKey(islistesi,blank=True,null=True)
    cari = models.ForeignKey(carikart,blank=True,null=True) 
    transfer_edildi = models.NullBooleanField(blank=True,null=True)
    transfer_kul = models.ForeignKey(MyUser,related_name="Transfer eden",blank=True,null=True)
    transfer_timestamp = models.DateTimeField(blank=True,null=True)
    fis_mi = models.NullBooleanField(blank=True,null=True)
    kullanici = models.ForeignKey(MyUser,related_name="Olusturan")
    timestamp = models.DateTimeField()

    def __unicode__(self):
        return self.aciklama

class fatura_detay(models.Model):
    fatura = models.ForeignKey(fatura)
    aciklama = models.CharField(max_length=150)
    price = models.DecimalField(max_digits=11, decimal_places=4)
    cost_price = models.DecimalField(max_digits=11, decimal_places=4)
    quantity = models.IntegerField()
    unit= models.ForeignKey(birim)
    price_total = models.DecimalField(max_digits=11, decimal_places=4)
    tax_ratio = models.DecimalField(max_digits=4, decimal_places=2)
    tax_total = models.DecimalField(max_digits=11, decimal_places=4)
    discount_rate1 = models.DecimalField(max_digits=4, decimal_places=2,blank=True,null=True)
    discount_total1 = models.DecimalField(max_digits=11, decimal_places=4,blank=True,null=True)
    discount_rate2 = models.DecimalField(max_digits=4, decimal_places=2,blank=True,null=True)
    discount_total2 = models.DecimalField(max_digits=11, decimal_places=4,blank=True,null=True)
    coefficient = models.IntegerField()
    kullanici = models.ForeignKey(MyUser)
    timestamp = models.DateTimeField()
    
    def __unicode__(self):
        return self.aciklama

class Tag(models.Model):
    name = models.CharField(max_length=150)
                             
    def __unicode__(self):
        return self.name

class Document(models.Model):
    cihaz = models.ForeignKey(cihazlar)
    aciklama = models.CharField(max_length=200, blank=True)
    document = models.FileField(upload_to=settings.MEDIA_ROOT, null=True, blank=True)   
    tag = models.ManyToManyField(Tag,null=True,blank=True)
    kullanici = models.ForeignKey(MyUser)
    timestamp = models.DateTimeField()
    
    def __unicode__(self):
        return self.aciklama

class tuketimnoktalari(models.Model):
    sno= models.IntegerField()
    aciklama = models.CharField(max_length=350)
    birim = models.ForeignKey(birim)
    
    def __unicode__(self):
        return self.aciklama

class tuketimkaydi(models.Model):
    tuketimnoktasi = models.ForeignKey(tuketimnoktalari)
    deger = models.DecimalField(max_digits=11, decimal_places=4)
    eknot = models.CharField(blank=True,null=True,max_length=350)
    
    def __unicode__(self):
        try:
            return tuketimnoktalari.objects.get(id=self.tuketimnoktasi_id).aciklama
        except:
            return unicode(tuketimnoktalari.objects.get(id=self.tuketimnoktasi_id).aciklama).encode('ascii')

class tuketimtablosu(models.Model):
    aciklama = models.CharField(max_length=350)
    tarih = models.DateField()
    kayit = models.ManyToManyField(tuketimkaydi)
    fatura = models.ForeignKey(fatura,blank=True,null=True)
    kullanici = models.ForeignKey(MyUser)
    timestamp = models.DateTimeField('date_created') 
    onaylandi = models.NullBooleanField(blank=True,null=True)
    
    
class sabittablolar(models.Model):
    tablo_adi = models.CharField(max_length=150)
    adres= models.CharField(max_length=250)
    aciklama = models.CharField(max_length=150)
    kategori = models.CharField(max_length=150)
    
    def __unicode__(self):
        return self.aciklama + " ( Tablo : " + self.tablo_adi + " )"
    
class emaillogs(models.Model):
    isid = models.ForeignKey(islistesi)
    _subject = models.CharField(max_length=300,blank=True,null=True)
    _from = models.CharField(max_length=150)
    _to = models.CharField(max_length=150)
    _body = models.CharField(max_length=1000,blank=True,null=True)
    kullanici = models.ForeignKey(MyUser)
    timestamp = models.DateTimeField()
    
    